from PyQt5.Qt import (QWidget, QApplication, QLineEdit,
                      QPushButton, QHBoxLayout, QVBoxLayout, QLabel, QMessageBox, QFileDialog)
import sys, os
from glob import glob


def complete_task(file_type, src_path, target_path):
    # 根据文件类型，源路径，目标路径，进行文件批量迁移
    if file_type and src_path and target_path:
        files = glob(src_path + '\\*' + file_type)

        for file in files:
            filename = file.split('\\')[-1]
            os.replace(file, target_path + '\\' + filename)
        return 0
    return -1


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

        self.file_dialog_btn1.clicked.connect(lambda: self.file_dialog(1))
        self.file_dialog_btn2.clicked.connect(lambda: self.file_dialog(2))
        self.verify_btn.clicked.connect(self.task)

    def initUI(self):
        self.setFixedSize(340, 220)
        self.setWindowTitle("文件迁移")

        self.label1 = QLabel("文件类型:", self)
        self.label2 = QLabel("文件源路径:", self)
        self.label3 = QLabel("文件目标路径:", self)

        self.type = QLineEdit(self)
        self.src_path = QLineEdit(self)
        self.target_path = QLineEdit(self)

        self.file_dialog_btn1 = QPushButton("打开", self)
        self.file_dialog_btn2 = QPushButton("打开", self)
        self.verify_btn = QPushButton("确认", self)

        self.hbox1 = QHBoxLayout()
        self.hbox2 = QHBoxLayout()
        self.hbox3 = QHBoxLayout()
        self.hbox4 = QHBoxLayout()
        self.vbox1 = QVBoxLayout()
        self.vbox2 = QVBoxLayout()
        self.vbox3 = QVBoxLayout()

        self.hbox1.addWidget(self.type)
        self.hbox2.addWidget(self.src_path)
        self.hbox2.addWidget(self.file_dialog_btn1)
        self.hbox3.addWidget(self.target_path)
        self.hbox3.addWidget(self.file_dialog_btn2)

        self.vbox1.addWidget(self.label1)
        self.vbox1.addWidget(self.label2)
        self.vbox1.addWidget(self.label3)
        self.vbox2.addLayout(self.hbox1)
        self.vbox2.addLayout(self.hbox2)
        self.vbox2.addLayout(self.hbox3)

        self.hbox4.addLayout(self.vbox1)
        self.hbox4.addLayout(self.vbox2)

        self.vbox3.addLayout(self.hbox4)
        self.vbox3.addWidget(self.verify_btn)

        self.setLayout(self.vbox3)

    def task(self):
        file_type = self.type.text()
        src_path = self.src_path.text()
        target_path = self.target_path.text()
        flag = complete_task(file_type, src_path, target_path)
        dialog = QMessageBox(self)
        dialog.setWindowTitle("    ")
        if flag == 0:
            dialog.setText("完成！")
        else:
            dialog.setText("请输入必要的信息！")
        dialog.show()

    def file_dialog(self, flag):
        filename = QFileDialog.getExistingDirectory()
        if flag == 1:
            self.src_path.setText(filename)
            self.src_path.setReadOnly(True)
        elif flag == 2:
            self.target_path.setText(filename)
            self.target_path.setReadOnly(True)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    w = Window()
    w.show()
    sys.exit(app.exec_())
